package com.whl.sm.controller.parking;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.parking.ParkingBill;
import com.whl.sm.model.parking.ParkingLots;
import com.whl.sm.model.parking.ParkingRecord;
import com.whl.sm.service.parking.ParkingBillService;
import com.whl.sm.service.parking.ParkingLotsService;
import com.whl.sm.service.parking.ParkingRecordService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.vo.ParkingBillVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/parking-bill")
public class ParkingBillController extends BaseController {

    @Autowired
    private ParkingRecordService parkingRecordService;

    @Autowired
    private ParkingBillService parkingBillService;

    @Autowired
    private ParkingLotsService parkingLotsService;

    @Autowired
    private OrgUserService orgUserService;


    @RequestMapping("/list")
    public Result getParkingBill(HttpServletRequest request){
        String likeName = request.getParameter("likeName");
        Page<Object> resultPage = new Page<>();
        if (likeName!=null && !likeName.equals("undefined") && !likeName.equals("")){
            QueryWrapper recordQueryWrapper = getLikeParam("license_plate");
            recordQueryWrapper.eq("visible",0);//0表示离场
            List<ParkingRecord> relateRecord = parkingRecordService.list(recordQueryWrapper);
            QueryWrapper queryWrapper = new QueryWrapper();
            if (relateRecord.size()>0){
                List<String> plateIdList = relateRecord.stream().map(ParkingRecord::getId).collect(Collectors.toList());
                queryWrapper.in("parking_record_id",plateIdList);
                setHttpRequestQueryWrapper(queryWrapper,"parking_lots_id");
                queryWrapper.ne("is_del",1);
                queryWrapper.orderByDesc("visible");
                resultPage = parkingBillService.page(getPage(),queryWrapper);
            }

        }
        else {
            QueryWrapper queryWrapper = new QueryWrapper();
            setHttpRequestQueryWrapper(queryWrapper,"parking_lots_id");
            queryWrapper.ne("is_del",1);
            queryWrapper.orderByDesc("visible");
            resultPage = parkingBillService.page(getPage(),queryWrapper);

        }
        List<Object> returnVo = resultPage.getRecords().stream().map(obj->{
            ParkingBill parkingBill = (ParkingBill) obj;
            ParkingLots parkingLots = parkingLotsService.getById(parkingBill.getParkingLotsId());
            ParkingRecord parkingRecord = parkingRecordService.getById(parkingBill.getParkingRecordId());
            return new ParkingBillVo(parkingBill,parkingRecord,parkingLots);
        }).collect(Collectors.toList());
        resultPage.setRecords(returnVo);

        return Result.ok(resultPage);
    }

    @RequestMapping("/getByCurrent")
    public Result getByCurrent(){
        String userId = orgUserService.getCurrentUserId();
        Page<Object> resultPage = new Page<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("payer",userId);
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("visible");
        resultPage = parkingBillService.page(getPage(),queryWrapper);
        List<Object> returnVo = resultPage.getRecords().stream().map(obj->{
            ParkingBill parkingBill = (ParkingBill) obj;
            ParkingLots parkingLots = parkingLotsService.getById(parkingBill.getParkingLotsId());
            ParkingRecord parkingRecord = parkingRecordService.getById(parkingBill.getParkingRecordId());
            return new ParkingBillVo(parkingBill,parkingRecord,parkingLots);
        }).collect(Collectors.toList());
        resultPage.setRecords(returnVo);
        return Result.ok(resultPage);
    }


    @RequestMapping("/getBillPrice/{recordId}")
    public Result getBillPrice(@PathVariable("recordId") String recordId){
        return parkingBillService.getBillPrice(recordId);
    }

    @RequestMapping("/getBillPriceByPlate")
    public Result getBillPriceByPlate(@RequestParam("licensePlate") String licensePlate){
        return parkingBillService.getBillPriceByPlate(licensePlate);
    }


    @RequestMapping("/buildBill/{recordId}")
    public Result addBill(@PathVariable("recordId") String recordId){
        return parkingBillService.addRecordBill(recordId);
    }





    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            parkingBillService.update(new UpdateWrapper<ParkingBill>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }

        return Result.ok();
    }


}
